import json
import pprint
import subprocess
from functools import partial
subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')

import execjs
import requests

cookies = {
    'abRequestId': '962b9322-e85f-533a-bac7-b704b8e713c1',
    'xsecappid': 'xhs-pc-web',
    'a1': '18bb2c116d1davozu4yc2t8yyzy4bdk2lxv980kx450000105644',
    'webId': 'bbfb0cce5cab1fff7ddff6b1741ed1c4',
    'web_session': '030037a27f2f5e5014bc673d34224a8d62ef99',
    'gid': 'yYDDJSyJ281yyYDDJSyyKC8Tfyf0hlu74vSJhC49Yvvuvj284DfTJY888y82K448Y8YqWKy0',
    'webBuild': '4.17.2',
    'unread': '{%22ub%22:%22664574c5000000001e02c502%22%2C%22ue%22:%2266385aa1000000001e037c3a%22%2C%22uc%22:25}',
    'acw_tc': '21b8b0cc0402a0425bd4eec9135c923666af3beac4fd6337f1e1f7772c171ef5',
    'websectiga': '984412fef754c018e472127b8effd174be8a5d51061c991aadd200c69a2801d6',
    'sec_poison_id': '5c8aa19c-d63d-4bfe-91ad-b8e0a840c077',
}

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9,en-GB;q=0.8,en-US;q=0.7,en;q=0.6',
    'cache-control': 'no-cache',
    'content-type': 'application/json;charset=UTF-8',
    'cookie': 'abRequestId=962b9322-e85f-533a-bac7-b704b8e713c1; xsecappid=xhs-pc-web; a1=18bb2c116d1davozu4yc2t8yyzy4bdk2lxv980kx450000105644; webId=bbfb0cce5cab1fff7ddff6b1741ed1c4; web_session=030037a27f2f5e5014bc673d34224a8d62ef99; gid=yYDDJSyJ281yyYDDJSyyKC8Tfyf0hlu74vSJhC49Yvvuvj284DfTJY888y82K448Y8YqWKy0; webBuild=4.17.2; websectiga=3633fe24d49c7dd0eb923edc8205740f10fdb18b25d424d2a2322c6196d2a4ad; sec_poison_id=02d340f8-7ab1-45db-ae79-f1a9e206a3bd; acw_tc=499211ea243ef26318e64918384c0afa2f35574003e62a05297345177f4f76c5; unread={%22ub%22:%22664574c5000000001e02c502%22%2C%22ue%22:%2266385aa1000000001e037c3a%22%2C%22uc%22:25}',
    'origin': 'https://www.xiaohongshu.com',
    'pragma': 'no-cache',
    'priority': 'u=1, i',
    'referer': 'https://www.xiaohongshu.com/',
    'sec-ch-ua': '"Google Chrome";v="125", "Chromium";v="125", "Not.A/Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36',
    'x-b3-traceid': '41ea387a272326e9',
    # 'x-s': 'XYW_eyJzaWduU3ZuIjoiNTEiLCJzaWduVHlwZSI6IngxIiwiYXBwSWQiOiJ4aHMtcGMtd2ViIiwic2lnblZlcnNpb24iOiIxIiwicGF5bG9hZCI6IjIyN2FiZmJiNjMyMGZhOWEyMDJlN2Y1OGU0NzQxOTQxMWI1NDE0NDI2ZGRkOTU5ZDE4NGMzYzIxZmRiZDJkZTE2NjU5ZGE5ZTAxNDkzYWZiZWUzNjM2NmE0ODZlN2Y2MmM5ZTNiZmRhMWZhYTFlYjkwZDc0YWEzMWI1NGM3MmNkMGQ3NGFhMzFiNTRjNzJjZGFjNDg5YjlkYThjZTVlNDhmNGFmYjlhY2ZjM2VhMjZmZTBiMjY2YTZiNGNjM2NiNWU4OTIxZDAxNTcwYTc4MTIwMTFlZjQ0YWQxYWVmYjc2YmQyOWY4OTg3MWNjNTc5ZTkxOWQ3ODIwMDkxZjc1NDY0OGRiYzgzNWM3MWZiMWIwNGNhMjc2MDI3NDQxZGMwODFmYjYwMmViYWI2Yjc1M2U1NzIzYzVkYjg4NzYzOTc0ZWUwZGUwNWFlNzI1ZTdhNDFiZTYwMTczOTY4YTVkYzQ3MjgwNDBlNjhlOTk3NWY4MjljODAxOGVlMWQ0M2M3YyJ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-t': '1716898791705',
}

# 路径
c = '/api/sns/web/v1/homefeed'
json_data = {
    'cursor_score': '',
    'num': 18,
    'refresh_type': 1,
    'note_index': 35,
    'unread_begin_note_id': '',
    'unread_end_note_id': '',
    'unread_note_count': 0,
    'category': 'homefeed.fashion_v3',
    'search_key': '',
    'need_num': 8,
    'image_formats': [
        'jpg',
        'webp',
        'avif',
    ],
    'need_filter_image': False,
}
data = json.dumps(json_data, separators=(',', ':'))
# print(data)
params = execjs.compile(open('xhs.js', 'r', encoding='utf-8').read()).call('get_x_s', c, json_data)
# print(params)
headers['x-s'] = params['X-s']
headers['x-t'] = str(params['X-t'])   #生成的时间戳是int类型,需要转化为字符串类型
response = requests.post('https://edith.xiaohongshu.com/api/sns/web/v1/homefeed', cookies=cookies, headers=headers,
                         data=data)
pprint.pprint(response.json())
